//==================================================================
//  Copyright (C) 2006-2007  Davide Pasca
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//==================================================================
///
///
///
///
///
//==================================================================


#ifndef CONSOLE_H
#define CONSOLE_H

#include <stdarg.h>
#include "pwindow.h"

//==================================================================
#define CONS_LINE_MAX_CHARS		256
#define CONS_MAX_LINES			256
#define CONS_MAX_COMMANDS		256
#define CONS_MAX_CMD_NAME_CHARS	16

#define CONS_HIST_MAX_LINES		256

//==================================================================
typedef void	(*cons_cmd_cb)( void *userp, TCHAR *paramsp[], int n_params );

//==================================================================
struct console_cmd_item_t
{
	TCHAR		name[CONS_MAX_CMD_NAME_CHARS];
	cons_cmd_cb	callback;
	void		*userdatap;
	const TCHAR	*helplinep;
};

//==================================================================
struct cons_cmd_def_t
{
	TCHAR		*namep;
	cons_cmd_cb	callback;
	const TCHAR	*helplinep;
};

//==================================================================
#define CONS_LINE_FLG_USER	1

//==================================================================
struct Console
{
	//==================================================================
	class TextLine
	{
	public:
		TCHAR	txt[ CONS_LINE_MAX_CHARS ];
		int		flags;

		TextLine() :
			flags(0)
		{
			txt[0] = 0;
		}
	};

	//==================================================================
	typedef void	(*LineCallback)( void *userp, const TCHAR *txtp, int is_cmd );

	Window				_win;
	TCHAR				_edit_line_txt[CONS_LINE_MAX_CHARS];
	int					_edit_cursor_pos;

	void				*_userdatap;

	FontUni				*_fontp;

	LineCallback		_linecb;

	TextLine			*_lines[CONS_MAX_LINES];
	int					_n_lines;
	int					_first_line_idx;

	TCHAR				*_hist_lines_ap[CONS_HIST_MAX_LINES];
	int					_n_hist_lines;
	int					_first_hist_line_idx;
	int					_cur_hist_line;

	int					_n_commands;
	console_cmd_item_t	_cmd_items[CONS_MAX_COMMANDS];

public:
	Console( Window *parent_winp, void *userdatap );
	~Console();

	void	SetLineCallback( LineCallback linecb );
	void	AddCommand( TCHAR *namep, cons_cmd_cb callback, const TCHAR *helplinep );
	void	AddCommandDefs( cons_cmd_def_t definitions[] );
	TCHAR	*cons_editline_get();
	void	cons_editline_set( const TCHAR *editlinep );
	void	execCmd( const TCHAR *cmdlinep );
	void	cons_cmd_execute_printf( const TCHAR *cmdfmtp, ... );
	int		IsShowing(void);
	void	Show( int onoff );
	void	cons_setfocus();
	void	PutLine( const TCHAR *txtp );
	void	cons_line_vprintf( const TCHAR *strp, va_list valist );
	void	LinePrintF( const TCHAR *strp, ... );

	int		cons_parent_eventfilter( void *userobjp, WindowEvent *eventp );


private:
	void	draw_edit_line();
	void	do_paint();
	void	_cons_line_puts( const TCHAR *txtp, int flags );
	void	hist_line_add( const TCHAR *txtp );
	int		find_cmd_idx( const TCHAR *cmdnamep );
	void	handle_new_line();
	void	handle_keydown( WindowEvent *eventp );

	static int	eventfilter_s( Console *mythis, WindowEvent *eventp );
	int			eventfilter( WindowEvent *eventp );
};

//==================================================================

#endif
